//
//  YXPictureView.swift
//  微博YX
//
//  Created by apple on 15/11/28.
//  Copyright © 2015年 lyx. All rights reserved.
//

import UIKit

class YXPictureView: UICollectionView {
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //    微博模型
    var status: YXStatus?{
        didSet{
            //   刷新
            reloadData()
        }
    }
    let ReuseIdentifier = "ReuseIdentifier"
    //    流水布局
    private let flowLayout = UICollectionViewFlowLayout()
    
    
    init(){
        //        设置布局
        super.init(frame:CGRectZero , collectionViewLayout: flowLayout)
        //    注册
        self.registerClass(YXPictureViewCell.self, forCellWithReuseIdentifier: ReuseIdentifier)
//        self.backgroundColor = UIColor.redColor()
        //        设置代理
        self.dataSource = self
    }
    
    //    计算行高
    func calcViewSize() -> CGSize{
        //        设置间距
        let padding: CGFloat = 10
        let itemSize = CGSize(width: 90, height: 90)
        flowLayout.itemSize = itemSize
//        流水布局的间距
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        //        获得图片的数量
        let count = status?.pictureURLs?.count ?? 0
        print("count:\(count)")
        //        判断是否有图片
        if count == 0 {
            return CGSizeZero
        }
        
//        1张
        if count == 1{
            let size = CGSize(width: 150, height: 120)
            flowLayout.itemSize = size
            return size
        }
        
        //        大于1时要设置间距
        flowLayout.minimumInteritemSpacing = 10
        flowLayout.minimumLineSpacing = 10
        
//        4张
        if count == 4 {
            let width = 2 * itemSize.width + padding
            
            // 设置配图视图的大小为size
            return CGSize(width: width, height: width)
            
        }
        
        //        其他数量都一样
        let col = 3
        let row = (count + col - 1) / col
        
        // 宽度: 列数 * itemSize.width + (列数 - 1) * 间距
        let width = CGFloat(col) * itemSize.width + (CGFloat(col) - 1) * padding
        
        // 高度: 行数 * itemSize.width + (行数 - 1) * 间距
        let height = CGFloat(row) * itemSize.width + (CGFloat(row) - 1) * padding
        
        return CGSize(width: width, height: height)
    }


    

}


//配图view的代理方法
extension YXPictureView: UICollectionViewDataSource{
    
    //    返回cell的数量
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return status?.pictureURLs?.count ?? 0
    }
    //    返回cell的内容
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ReuseIdentifier, forIndexPath: indexPath) as! YXPictureViewCell
        //        给cell的图片属性赋值
        cell.imageURL = status?.pictureURLs?[indexPath.item]
        print("cell:\(status?.pictureURLs?[indexPath.item])")
        return cell
    }
    
    
    
}



/// :MARK - 自定义的cell
class YXPictureViewCell: UICollectionViewCell{
    //    图片的url
    var imageURL: NSURL?{
        didSet{
            // 使用SDWebImage来加载图片
            bkImageView.sd_setImageWithURL(imageURL)
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
//        backgroundColor = UIColor.redColor()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //    准备UI
    func prepareUI(){
        
        //        添加图片
        contentView.addSubview(bkImageView)
        //        添加约束
        bkImageView.ff_Fill(contentView)
        
    }
    
    
    /// MARK:- 懒加载
    private lazy var bkImageView: UIImageView = {
        let img = UIImageView()
        /*
        case ScaleToFill        // Scale会缩放
        case ScaleAspectFit
        case ScaleAspectFill
        case Redraw
        case Center
        case Top
        case Bottom
        case Left
        case Right
        case TopLeft
        case TopRight
        case BottomLeft
        case BottomRight
        */
        
        // 设置图片填充模式
        img.contentMode = UIViewContentMode.ScaleAspectFill
        //        超出边界的要裁剪掉6
        img.layer.masksToBounds = true

        return img
    }()

    
}